﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
using System.Threading;
using System.Diagnostics;

namespace Rsa
{
    /// <summary>
    /// Interaction logic for JanelaLista.xaml
    /// </summary>
    public partial class JanelaLista : UserControl
    {
        private LancarThreads _lancar = null;
        private List<Lista> ListaEmail = new List<Lista>();
        private List<Lista> ListaTwitter = new List<Lista>();
        private List<Lista> ListaMoodle = new List<Lista>();
        private List<Lista> ListaEmenta = new List<Lista>();
        private List<Lista> ListaFacebook = new List<Lista>();
        private List<Lista> ListaTotal = new List<Lista>();
       

        public JanelaLista()
        {
            InitializeComponent();

            //listaBox.Items.Add(new Lista("ola", "nao sei", "../../imagens/twitter.png"));
            //listaBox.Items.Add(new Lista("1novo test", "blasd bla", "../../imagens/cantina.png"));
            
            emailLoading.Visibility = System.Windows.Visibility.Hidden;
            twitterLoading.Visibility = System.Windows.Visibility.Hidden;
            moodleLoading.Visibility = System.Windows.Visibility.Hidden;
            ementaLoading.Visibility = System.Windows.Visibility.Hidden;
            facebookLoading.Visibility = System.Windows.Visibility.Hidden;
        }


        public void sortData()
        {
            ListaTotal.Sort((x, y) => DateTime.Compare(y.Data, x.Data));
        }


        #region atualizar
        public void atualizarEmail(List<Lista> myLista)
        {
            if (myLista != null)
            {
                ListaEmail = myLista;
                atualizaListaBox();
            }
            emailLoading.Visibility = System.Windows.Visibility.Hidden;
            emailButton.IsEnabled = true;
            comboBoxEmail.IsEnabled = true;
            rectangle1.Fill = new SolidColorBrush(Color.FromRgb(255, 129, 14));
        }

        public void atualizarTwitter(List<Lista> myLista)
        {
            if (myLista != null)
            {
                ListaTwitter = myLista;
                atualizaListaBox();
            }
            twitterButton.IsEnabled = true;
            twitterLoading.Visibility = System.Windows.Visibility.Hidden;
            comboBoxTwitter.IsEnabled = true;
            rectangle2.Fill = new SolidColorBrush(Color.FromRgb(0, 69, 117));
        }

        public void atualizarMoodle(List<Lista> myLista)
        {
            if (myLista != null)
            {
                ListaMoodle = myLista;
                atualizaListaBox();
            }
            moodleButton.IsEnabled = true;
            moodleLoading.Visibility = System.Windows.Visibility.Hidden;
            comboBoxMoodle.IsEnabled = true;
            rectangle3.Fill = new SolidColorBrush(Color.FromRgb(247, 181, 0));
        }


        public void atualizarEmenta(List<Lista> myLista)
        {
            //Debug.WriteLine("entrou aqui" + myLista.ToString());
            if (myLista != null)
            {
                ListaEmenta = myLista;
                atualizaListaBox();
            }
            ementaButton.IsEnabled = true;
            ementaLoading.Visibility = System.Windows.Visibility.Hidden;
            comboBoxEmenta.IsEnabled = true;
            rectangle4.Fill = new SolidColorBrush(Color.FromRgb(155, 197, 54));
        }

        public void atualizarFacebook(List<Lista> myLista)
        {
            if (myLista != null)
            {
                ListaFacebook = myLista;
                atualizaListaBox();
            }
            facebookButton.IsEnabled = true;
            facebookLoading.Visibility = System.Windows.Visibility.Hidden;
            comboBoxFacebook.IsEnabled = true;
            rectangle5.Fill = new SolidColorBrush(Color.FromRgb(59, 89, 152));
        }

        public void atualizaListaBox()
        {

            ListaTotal.Clear();

            
            foreach (Lista i in ListaEmail)
            {
                ListaTotal.Add(i);
            }
            foreach (Lista i in ListaTwitter)
            {
                ListaTotal.Add(i);
            }
            foreach (Lista i in ListaMoodle)
            {
                ListaTotal.Add(i);
            }
            foreach (Lista i in ListaEmenta)
            {
                ListaTotal.Add(i);
            }

            foreach (Lista i in ListaFacebook)
            {
                ListaTotal.Add(i);
            }


            //ordenar por data
            this.sortData();

            //adicionar todos á listaBox
            listaBox.Items.Clear();
            string format = "dd-MM-yyyy HH:mm"; //"M d h:mm yy"
            foreach (Lista i in ListaTotal)
            {
                i.DataString = i.Data.ToString(format);
                listaBox.Items.Add(i);
            }
            

        }
        #endregion

        public LancarThreads Lancar
        {
            get { return _lancar; }
            set
            {
                _lancar = value;
                Lancar.emailEvento += new LancarThreads.EmailHandler(atualizarEmail);
                Lancar.twitterEvento += new LancarThreads.TwitterHandler(atualizarTwitter);
                Lancar.moodleEvento += new LancarThreads.MoodleHandler(atualizarMoodle);
                Lancar.ementaEvento += new LancarThreads.EmentaHandler(atualizarEmenta);
                Lancar.facebookEvento += new LancarThreads.FacebookHandler(atualizarFacebook);
            }
        }

        private void twitterButton_Click(object sender, RoutedEventArgs e)
        {
            twitterButton.IsEnabled = false;
            twitterLoading.Visibility = System.Windows.Visibility.Visible;
            comboBoxTwitter.IsEnabled = false;
            Lancar.startTwitter(comboBoxTwitter.SelectedIndex);
        }

        private void moodleButton_Click(object sender, RoutedEventArgs e)
        {
            moodleButton.IsEnabled = false;
            moodleLoading.Visibility = System.Windows.Visibility.Visible;
            comboBoxMoodle.IsEnabled = false;
            Lancar.startMoodle(comboBoxMoodle.SelectedIndex);
        }

        private void listaBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            try {
            var si = listaBox.SelectedItem as Lista;
            if (si.Data.ToString() != null)
                Console.WriteLine(si.Tipo.ToString());
                
            webBrowserCorpo.NavigateToString(si.Corpo);

            } catch(Exception x){
                Console.WriteLine("listaBox_SelectionChanged exception:" + x);
            }  
        }

        private void emailButton_Click(object sender, RoutedEventArgs e)
        {
            emailButton.IsEnabled = false;
            Lancar.BuscarEmail = 1;
            emailLoading.Visibility = System.Windows.Visibility.Visible;
            comboBoxEmail.IsEnabled = false;
            Lancar.startEmail(comboBoxEmail.SelectedIndex);
        }

        private void ementaButton_Click(object sender, RoutedEventArgs e)
        {
            ementaButton.IsEnabled = false;
            ementaLoading.Visibility = System.Windows.Visibility.Visible;
            comboBoxEmenta.IsEnabled = false;
            Lancar.startEmenta(comboBoxEmenta.SelectedIndex);
        }

        private void facebookButton_Click(object sender, RoutedEventArgs e)
        {
            facebookButton.IsEnabled = false;
            facebookLoading.Visibility = System.Windows.Visibility.Visible;
            comboBoxFacebook.IsEnabled = false;
            Lancar.startFacebook(comboBoxFacebook.SelectedIndex);
        }

        private void button_limpar_lista_Click(object sender, RoutedEventArgs e)
        {
            listaBox.Items.Clear();
        }





    }
}
